package com.iretailer.report.quartz.bosPos;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.iretailer.report.util.TimeUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class RestUtil {

    private static final Logger log = LoggerFactory.getLogger(RestUtil.class);

    private String url;
    private String sipKey;
    private String secret;

    public RestUtil(String url, String sipKey, String secret) {
        this.url = url;
        this.sipKey = sipKey;
        this.secret = secret;
    }

    private boolean check() {
        if (StringUtils.isBlank(url) ||
                StringUtils.isBlank(sipKey) ||
                StringUtils.isBlank(secret)) {

            return false;
        } else {
            return true;
        }
    }


    // 数据加密
    public String rest(String command, JSONObject cmdparam) throws Exception {
        if (!check()) {
            log.error("RestUtil [url sipKey secret] is not null");
            return null;
        }
        JSONArray tras = createTras(command, cmdparam);
        String tt = TimeUtil.localDateTime_format(LocalDateTime.now(), "yyyy-MM-dd HH:mm:ss.SSS");

        String m = SecureUtil.md5(secret);
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("sip_appkey", sipKey);
        params.put("sip_timestamp", tt);
        params.put("sip_sign", SecureUtil.md5(sipKey + tt + m));
        params.put("transactions", tras.toString());
        String queryString = delimit(params.entrySet(), true);

        return HttpUtil.createPost(url).body(queryString.getBytes()).execute().body();
    }

    // 组成业务数据
    private static JSONArray createTras(String command, JSONObject cmdparam) throws JSONException {
        JSONObject tra = new JSONObject();
        tra.put("id", 1);
        tra.put("command", command);
        tra.put("params", cmdparam);
        JSONArray ja = new JSONArray();
        ja.add(tra);
        return ja;
    }

    // 生成querystring
    private String delimit(Collection<Map.Entry<String, String>> entries,
                           boolean doEncode) throws Exception {
        if (entries == null || entries.isEmpty()) {
            return null;
        }
        StringBuffer buffer = new StringBuffer();
        boolean notFirst = false;
        for (Map.Entry<String, ?> entry : entries) {
            if (notFirst) {
                buffer.append("&");
            } else {
                notFirst = true;
            }
            Object value = entry.getValue();
            if (value == null) value = "";
            buffer.append(doEncode ? URLEncoder.encode(entry.getKey(), "UTF8") : entry.getKey()).append("=").append(
                    doEncode ? URLEncoder.encode(value.toString(), "UTF8") : value);
        }
        return buffer.toString();
    }

}
